package com.kirk.cts.crawler.view;

import com.kirk.cts.core.cache.UserCacheManager;
import com.kirk.cts.core.event.CrawlerToolListener;
import com.kirk.cts.crawler.constants.BaseConstants;
import com.kirk.cts.crawler.domain.NetProxySettingBean;
import com.kirk.cts.ui.view.dialog.AbstractDialog;
import org.apache.commons.lang.StringUtils;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * <p> NetProxySettingDialog.java </p>
 *
 * @version 1.0.0
 * @Author kirkliu
 * @Date 2020-08-29 22:44
 * @Description 网络代理设置
 */
public class NetProxySettingDialog extends AbstractDialog implements CrawlerToolListener {

    private static final long serialVersionUID = 1L;

    private static NetProxySettingDialog instance;

    private NetProxySettingBean netSettingBean;
    /**
     * 用户缓存管理类
     */
    protected UserCacheManager userCacheManager = UserCacheManager.getInstance();

    private NetProxySettingDialog() {
        super(300, 280, true);
        this.setUndecorated(true);
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                checkValue();
                save();
            }
        });
        getFinishButton().setText("保存");
        getCancelButton().setText("取消");
    }

    /**
     * @return the instance
     */
    public static NetProxySettingDialog getInstance() {
        if (null == instance) {
            instance = new NetProxySettingDialog();
        }
        return instance;
    }

    @Override
    public JComponent getCenterPane() {
        if (centerPane == null) {
            centerPane = NetProxySettingPanel.getInstance();
        }
        return centerPane;
    }

    @Override
    protected void finishButtonActionPerformed(ActionEvent event) {
        if (checkValue()) {
            save();
            this.dispose();
        }
    }

    /**
     * 校验参数
     */
    private boolean checkValue() {
        netSettingBean = NetProxySettingPanel.getInstance().getData();

        return true;
    }

    /**
     * 建立连接
     */
    private void save() {
        userCacheManager.setValue(BaseConstants.NET_PROXY_SETTING_HOST, netSettingBean.getProxyHost());
        userCacheManager.setValue(BaseConstants.NET_PROXY_SETTING_PORT, netSettingBean.getProxyPort());
        userCacheManager.setValue(BaseConstants.NET_PROXY_SETTING_USERNAME, netSettingBean.getUserName());
        userCacheManager.setValue(BaseConstants.NET_PROXY_SETTING_PASSWORD, netSettingBean.getPassword());
        userCacheManager.setValue(BaseConstants.NET_PROXY_SETTING_DOMAIN, netSettingBean.getDomain());
        userCacheManager.setValue(BaseConstants.NET_PROXY_SETTING_ISUSED, netSettingBean.getIsUsed());
    }


    protected void initData(String type) {
        netSettingBean = new NetProxySettingBean();
        netSettingBean.setProxyHost(userCacheManager.getValue(BaseConstants.NET_PROXY_SETTING_HOST));
        netSettingBean.setProxyPort(userCacheManager.getValue(BaseConstants.NET_PROXY_SETTING_PORT));
        netSettingBean.setUserName(userCacheManager.getValue(BaseConstants.NET_PROXY_SETTING_USERNAME));
        netSettingBean.setPassword(userCacheManager.getValue(BaseConstants.NET_PROXY_SETTING_PASSWORD));
        netSettingBean.setDomain(userCacheManager.getValue(BaseConstants.NET_PROXY_SETTING_DOMAIN));
        if (StringUtils.isNotBlank(userCacheManager.getValue(BaseConstants.NET_PROXY_SETTING_ISUSED))) {
            netSettingBean.setIsUsed(userCacheManager.getValue(BaseConstants.NET_PROXY_SETTING_ISUSED));
        } else {
            netSettingBean.setIsUsed("false");
        }
        fillJTabbedPane();
    }

    public void dispose() {
        super.dispose();
        centerPane = null;
    }

    /**
     * 填充JTabbedPane值
     */
    private void fillJTabbedPane() {
        logger.info("网络代理连接参数设置");
        NetProxySettingPanel.getInstance().initData(netSettingBean);
    }


}
